//
//  FlutterPermissionImpl.swift
//  Runner
//
//  Created by 莫华军 on 2024/9/21.
//

import Foundation
import Photos
import AVFoundation
import MediaPlayer
import Flutter

//权限申请封装
class FlutterPermissionImpl{
    private var primissionMethodChannel: FlutterMethodChannel?
    
    
    static let sharedInstance = FlutterPermissionImpl()
    
    private init() {
    }
    
    func setFlutterBinaryMessenger(binaryMessenger:FlutterBinaryMessenger){
        //权限相关
        primissionMethodChannel = FlutterMethodChannel(name: "permission_method_channel", binaryMessenger: binaryMessenger)
        primissionMethodChannel?.setMethodCallHandler { (call:FlutterMethodCall, result:@escaping FlutterResult) in
            if (call.method == "hasPhotoPermission") {
                // 获取当前的照片库访问权限状态
                let status = PHPhotoLibrary.authorizationStatus()
                switch status {
                case .notDetermined:
                    // 如果权限状态未确定，请求访问权限
                    /*PHPhotoLibrary.requestAuthorization { status in
                     // 处理授权结果
                     if status == .authorized {
                     print("Photo library access granted.")
                     result(["isDenied": false])
                     } else {
                     print("Photo library access denied.")
                     result(["isDenied": true])
                     }
                     }*/
                    result(["isDenied": false])
                case .restricted, .denied:
                    // 如果权限被限制或被拒绝，显示错误信息
                    print("Photo library access is restricted or denied.")
                    result(["isDenied": true])
                case .authorized:
                    // 如果权限已经被授予，继续执行需要访问照片库的操作
                    print("Photo library access already granted.")
                    result(["isDenied": false])
                @unknown default:
                    // Swift 5.1 引入的未知成员处理
                    print("Unknown authorization status.")
                    result(["isDenied": true])
                    
                }
            }else if(call.method == "hasCameraPermission"){
                let captureDevice = AVCaptureDevice.default(for: .video)
                let cameraPermissionStatus = AVCaptureDevice.authorizationStatus(for: .video)
                
                switch cameraPermissionStatus {
                case .notDetermined:
                    print("Camera permission not determined yet.")
                    // 请求相机权限
                    AVCaptureDevice.requestAccess(for: .video) { granted in
                        if granted {
                            print("Camera access granted.")
                            result(["isDenied": false])
                        } else {
                            print("Camera access denied.")
                            result(["isDenied": true])
                        }
                    }
                case .restricted:
                    print("Camera access is restricted.")
                    result(["isDenied": true])
                case .denied:
                    print("Camera access is denied.")
                    result(["isDenied": true])
                case .authorized:
                    print("Camera access is authorized.")
                    result(["isDenied": false])
                @unknown default:
                    print("Unknown camera access status.")
                    result(["isDenied": true])
                }
            }else if(call.method == "hasMicPermission"){
                let microphonePermissionStatus = AVCaptureDevice.authorizationStatus(for: .audio)
                
                switch microphonePermissionStatus {
                case .notDetermined:
                    print("Microphone permission not determined yet.")
                    // 请求麦克风权限
                    AVCaptureDevice.requestAccess(for: .audio) { granted in
                        if granted {
                            print("Microphone access granted.")
                            result(["isDenied": false])
                        } else {
                            print("Microphone access denied.")
                            result(["isDenied": true])
                        }
                    }
                case .restricted:
                    print("Microphone access is restricted.")
                    result(["isDenied": true])
                case .denied:
                    print("Microphone access is denied.")
                    result(["isDenied": true])
                case .authorized:
                    print("Microphone access is authorized.")
                    result(["isDenied": false])
                @unknown default:
                    print("Unknown microphone access status.")
                    result(["isDenied": true])
                }
            }
            else if(call.method == "hasMusicPermission"){
                MPMediaLibrary.requestAuthorization { status in
                    switch status {
                    case .notDetermined:
                        result(["isDenied": false])
                    case .restricted:
                        print("Microphone access is restricted.")
                        result(["isDenied": true])
                    case .denied:
                        print("Microphone access is denied.")
                        result(["isDenied": true])
                    case .authorized:
                        print("Microphone access is authorized.")
                        result(["isDenied": false])
                    @unknown default:
                        print("Unknown microphone access status.")
                        result(["isDenied": true])
                    }
                }
            }
            else if(call.method == "openAuthorized"){
                            //跳到授权界面
                            if let url = URL(string: UIApplication.openSettingsURLString) {
                                UIApplication.shared.open(url, options: [:], completionHandler: nil)
                            }
                        }
        }
    }
    
}
